from app import socketio
from flask_socketio import emit
from app.modules import Payload,Record,Project,Category,Session
from flask import current_app,request

@socketio.on('connect',namespace="/socketapi")
def handle_connect():
    cookie = request.cookies.get(current_app.config.get('SESSION_NAME'))
    if not Session.login_check(cookie):
        return False

@socketio.on('get_base_info',namespace="/socketapi")
def get_base_info(data):
    data = {}
    data['payloadList'] = Payload.get_all_payload_with_json()
    data['projectList'] = Project.get_all_project_info_with_json()
    data['categoryList'] = Category.get_all_category_with_json()
    data['recordList'] = []  # 只存储最近未访问 的record
    emit('set_base_info',data)

def update_base_info(type,command,data_list):
    # type指定要更新哪种数据，可用的值有：payload，project，category，record
    # command指定对这种数据的操作，可用的操作有：add，update，delete
    # 其中delete操作只需传递对应id即可
    ret = {'type':'','data':{'command':'','data_list':[]}}
    ret['type'] = type
    ret['data']['command'] = command
    ret['data']['data_list'] = data_list
    socketio.emit('update_base_info',ret,namespace="/socketapi")
